<template>
  <div class="detail" ref="jobDetail">
    <van-skeleton title avatar :row="18" avatar-size="17vmin" :loading="loading">
      <div class="top-wrap">
        <div class="job-name">
          <span class="name ellipsis">{{ info.name }}</span>
          <span class="pay">{{ info.payid }}-{{ info.pay_end }}k</span>
        </div>
        <div class="type">
          <span>{{ info.city_name }}</span>
          <span>{{ info.is_all === 0 ? '实习' : '全职' }}</span>
          <span>{{ info.eid }}</span>
        </div>
        <div class="tag">
         <span
             class="tag-item"
             v-for="(v, i) in company.welfare"
             :key="'welfare_' + i"
         >{{ v }}</span
         >
        </div>
        <div class="time">
          <i class="iconfont icon-time"></i>
          <span>申请截止：{{ info.end_time }}</span>
        </div>
      </div>
      <div class="company" @click="jumpTo">
        <div class="logo" :style="{
                background: 'url(' + company.logo + ') center center / contain no-repeat',
              }"></div>
        <div class="name">
          <p class="title ellipsis">{{ company.cname }}</p>
          <p class="type ellipsis">{{ company.industry_data }}</p>
        </div>
        <div class="btn-box" :class="[info.is_apply === 1 && 'isAdd']" @click.stop="toApply">{{ info.is_apply === 1 ? '投递成功' : '投递简历' }}</div>
      </div>
      <div class="requirement">
        <p class="detail-title">职位描述</p>
        <div class="desc">
          <div class="desc-container">
            <p class="desc-title">职责描述：</p>
            <div class="desc-content">
              <p v-html="info.duty"></p>
            </div>
            <p class="pos-title">岗位要求：</p>
            <div class="pos-content">
              <p v-html="info.request"></p>
            </div>
          </div>
        </div>
        <p class="detail-title">其他信息</p>
        <div class="other">
          <div class="other-item">
            <p>
              <span>工作地点：</span><span>{{ info.for_bumen }}</span>
            </p>
            <p>
              <span>专业要求：</span><span>{{ info.major }}</span>
            </p>
          </div>
        </div>
      </div>
    </van-skeleton>
  </div>
</template>

<script>
import { jobDetail, apply } from '@/api/company'

export default {
  name: 'Detail',
  components: {},
  data () {
    return {
      loading: true,
      id: '',
      info: {},
      company: {},
    }
  },
  created () {
  },
  mounted () {
    this.id = this.$route.query.id || ''
    this.fetchData()
  },
  methods: {
    fetchData () {
      jobDetail({
        id: this.id,
      }).then(res => {
        if (res.code === 200) {
          this.info = res.data && res.data.jobinfo
          this.company = res.data && res.data.company
          let timer = setTimeout(()=> {
            this.loading = false;
            clearTimeout(timer);
            timer = null;
          }, 600)
        } else {
          this.$alertErr(res.msg || '网络错误, 稍后再试！')
        }
      }).catch(err => {
        this.$alertErr(err)
      })
    },
    toApply () { // 申请
      if(this.info.is_apply === 1) {
        this.$alertWarn('已投递成功，无需重复投递！');
        return
      }
      apply({
        job_id: this.info.id,
        com_id: this.company.id,
      }).then(res => {
        if (res.code === 200) {
          this.info.is_apply === 1 ? this.info.is_apply = 0 : this.info.is_apply = 1
          this.$alertSuc(res.msg || '提交成功！');
        } else if (res.code === 2001) {
          this.$alertWarn('请到桌面端完善简历！');
        }  else if (res.code === 1008) {
          this.$alertWarn('请先登录！', ()=>{
            this.$router.replace({
              path: '/login',
              query: {
                link: 'job-detail?id='+ this.id
              }
            })
          });
        } else {
          this.$alertErr(res.msg || '网络错误, 稍后再试！');
        }
      }).catch(err => {
        console.error(err);
        this.$alertErr(err)
      })
    },
    jumpTo(){
      this.$router.push({
        path: 'company',
        query: {
          id: this.company.id
        }
      })
    },
  },

}
</script>

<style lang="stylus" scoped>
.detail {
  padding-bottom: 20px;

  .top-wrap {
    padding: 20px 0 20px 20px;
    background: #f6f7f8;

    .job-name {
      display: flex;
      font-size: 18px;
      height: 30px;
      align-items: center;

      .name {
        flex: 1;
        font-size: 20px;
        color: $color-text;
      }

      .pay {
        flex: 0 0 70px;
        font-size: 18px;
        text-align: right;
        color: $color-danger;
        margin-right: 20px;
      }
    }

    .type {
      margin: 10px 0 5px 0;

      span {
        margin-right: 10px;
        font-size: 12px;
        color: #666;
      }
    }

    .tag {
      width: 125%;
      transform: scale(.8);
      transform-origin left
      span {
        font-size: 12px;
        color: $color-main;
        padding: 0 8px;
        display: inline-block;
        height: 28px;
        line-height: 28px;
        border: 1px solid $color-main;
        border-radius: 4px;
        margin-right: 10px;
        margin-bottom: 10px;
      }
    }
    .time {
      padding-left: 10px;
      position: relative;
      height: 20px;
      line-height: 20px;
      &::before {
        content: '';
        height: 10px;
        width: 1px;
        position: absolute;
        left: 0;
        top: 5px;
        background: $color-main;
      }
      i,
      span {
        margin-right: 10px;
        font-size: 12px;
        color: #666;
      }
    }
  }
  .company {
    height: 70px;
    display flex
    align-items center
    padding:0 20px;
    border-bottom: 1px solid #f5f5f5;

    .logo {
      flex 0 0 48px
      height 48px
      width 48px
      font-size 0
      border-radius 50%
      border 1px solid #ebebeb
      background #fff
      overflow hidden

      img {
        width 100%
      }
    }
    .name {
      margin-left 5px
      flex 0 0 185px
      overflow hidden

      .title {
        font-size 12px
        color #333
      }

      .type {
        margin-top 5px
        font-size 10px
        color #999
      }
    }
    .btn-box {
      flex 0 0 100px
      height 30px
      line-height 30px
      text-align center
      color #fff
      border-radius 15px
      font-size 12px
      background $color-main
      &.isAdd {
        background #ccc
      }
    }
  }
  .requirement {
    padding: 30px 20px;

    .detail-title {
      color: #333;
      font-size: 16px;
      font-weight: 600;
    }

    .desc-title,
    .pos-title {
      color: #999;
      padding: 20px 0 10px 0;
      font-size: 14px;
    }

    .desc-content,
    .pos-content {
      margin-bottom: 40px;

      p {
        color: #666;
        font-size: 12px;
        line-height: 28px;
        white-space: normal;
      }
    }

    .other {
      margin: 10px 0;

      .other-item {
        p {
          line-height: 20px;
          display: inline-block;
          margin-right: 10px;
          margin-bottom: 10px;
          font-size: 12px;
          &:last-child {
            margin: 0;
          }

          span {
            white-space: normal;
            &:first-child {
              color: #999;
            }

            &:last-child {
              color: #666;
            }
          }
        }
      }
    }
  }
}
</style>

